/**
 * FileName: BusinessException
 * Author: Wb
 * Date: 2025/9/25 9:34
 * Description:
 * History:
 */
package org.test.device.architecture.exception;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.http.HttpStatus;
import org.test.device.utils.BusinessCode;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Wb
 * @create 2025/9/25
 * @since 1.0.0
 */
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BusinessException extends RuntimeException{

    private Long businessCode;

    private String businessType;

    /**
     * -- SETTER --
     *
     * @param message the message to set
     */
    private String message;

    private HttpStatus httpStatus;

    public BusinessException(BusinessCode businessCode) {
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();
    }

    public BusinessException(String message, final BusinessCode businessCode) {
        super(message);
        this.message = message;
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();    }

    public BusinessException(final String message, final BusinessCode businessCode, final HttpStatus httpStatus) {
        super(message);
        this.message = message;
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();
        this.httpStatus = httpStatus;
    }

    /**
     *
     * DUBBO使用FastJSON来反序列化异常时,
     * 会检查调用创建异常会检查这个构造方法来注入message内容,不然message内容会丢失
     * 可以查看fastJSON源码文件ThrowableDeserializer的createException方法看到处理方式
     */
    public BusinessException(final String message, final Throwable cause) {
        super(message, cause);
        this.message = message;
    }

    public BusinessException(final String message, final BusinessCode businessCode, final Throwable cause) {
        super(message, cause);
        this.message = message;
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();
    }

    /**
     * @return the message
     */
    @Override
    public String getMessage() {
        return message;
    }

    /**
     * @return the businessCode
     */
    public Long getBusinessCode() {
        return businessCode;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public String getBusinessType() {
        return businessType;
    }

}